package com.springboot.service.impl;

import com.springboot.model.User;
import com.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * UserServiceImpl
 *
 * @author jlicc
 * @create 2017-03-13-16:37
 **/
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    @Qualifier("primaryJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(Integer id,String name, Integer age) {
        jdbcTemplate.update("insert into TBL_SYS_USER(ID,NAME, AGE) values(?, ?, ?)",id, name, age);
    }
    @Override
    public void deleteByName(String name) {
        jdbcTemplate.update("delete from TBL_SYS_USER where NAME = ?", name);
    }
    @Override
    public Integer getAllUsers() {
        return jdbcTemplate.queryForObject("select count(1) from TBL_SYS_USER", Integer.class);
    }
    @Override
    public void deleteAllUsers() {
        jdbcTemplate.update("delete from TBL_SYS_USER");
    }

    @Override
    public User login(User user){
        return jdbcTemplate.queryForObject("select id,name,age from user where name = ?",User.class,user.getName());
    }
}
